package liuds.music.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;



import liuds.music.R;
import liuds.music.model.Music;
import liuds.music.utils.MusicUtils;

/**
 * Created by Administrator on 2018/5/2.
 */

public class MusicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    public List<Music> music;
    private int currentPlayIndex = -1;
    private OnMusicItemClickListener onMusicItemClickListener;

    public MusicAdapter(Context context,List<Music> music){
        this.context = context;
        this.music = music;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.music_item,null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onMusicItemClickListener!=null){
                    onMusicItemClickListener.onMusicItemClick(v);
                }
            }
        });
        return new MusicHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MusicHolder musicHolder = (MusicHolder)holder;
        if(currentPlayIndex == position){
            musicHolder.number.setVisibility(View.GONE);
            musicHolder.currentPlayIcon.setVisibility(View.VISIBLE);
        }else{
            musicHolder.currentPlayIcon.setVisibility(View.GONE);
            musicHolder.number.setVisibility(View.VISIBLE);
            musicHolder.number.setText((position+1)+"");
        }
        musicHolder.name.setText(music.get(position).getTitle());
        musicHolder.desc.setText(music.get(position).getSinger()+" - "+music.get(position).getAlbum());
        musicHolder.itemMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onMusicItemClickListener!=null){
                    onMusicItemClickListener.onMusicItemMenuClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return music.size();
    }

    private class MusicHolder extends RecyclerView.ViewHolder{

        private ImageButton currentPlayIcon,itemMenu;
        private TextView number,name,desc;

        public MusicHolder(View itemView) {
            super(itemView);
            currentPlayIcon = itemView.findViewById(R.id.currentPlayIcon);
            itemMenu = itemView.findViewById(R.id.itemMenu);
            number = itemView.findViewById(R.id.number);
            name = itemView.findViewById(R.id.name);
            desc = itemView.findViewById(R.id.desc);
        }
    }

    public void setOnMusicItemClick(OnMusicItemClickListener onMusicItemClickListener) {
        this.onMusicItemClickListener = onMusicItemClickListener;
    }

    public int getCurrentPlayIndex() {
        return currentPlayIndex;
    }

    public void setCurrentPlayIndex(int currentPlayIndex) {
        notifyItemChanged(this.currentPlayIndex);
        for(int i=0;i<MusicUtils.getPlayMusicList().size();i++){
            if(music.get(i).getId() == MusicUtils.getPlayMusicList().get(currentPlayIndex).getId()){
                this.currentPlayIndex = i;
                break;
            }
        }
        notifyItemChanged(this.currentPlayIndex);
    }

    public interface OnMusicItemClickListener{
        void onMusicItemClick(View v);
        void onMusicItemMenuClick(int position);
    }
}
